import { getTokenByCode } from '/api/user'
import { getInfo } from '/api/member'
import { getMchInfo, geoCode } from '/api/amap'
import Event from '/utils/Event';

const util = require('/utils/util.js')
import mchInfoObj from './mchInfo'

//app.js
App({
  event: new Event(),
  pinyin: require('/utils/pinyin-pro.min.js'),
  onLaunch: function (options) {
    var _this = this;
    _this.screenSize(); //获取可视窗口的尺寸
    wx.getSystemInfo({
      success: function (res) {
        _this.globalData.rpxR = 750 / res.windowWidth;
        _this.globalData.windowWidth = res.windowWidth * _this.globalData.rpxR;
        _this.globalData.windowHeight = res.windowHeight * _this.globalData.rpxR;
        _this.globalData.statusBarHeight = res.statusBarHeight * _this.globalData.rpxR;
      }
    })
    if (!util.isNull(options.xuid)) {
      _this.globalData.xuid = options.xuid;
    }
    _this.globalData.uuid = util.getUUID()
    // 检查是否登录
    wx.login({
      success: res => {
        console.log(res);
        // 发送 res.code 到后台换取 openId 并获取令牌
        getTokenByCode({
          mchId: _this.globalData.mchId,
          auth_code: res.code,
          uuid: _this.globalData.uuid,
          pluginId: 'weixinMiniAuthPlugin'
        }).then(res => {
          console.log(res, '获取token')
          if (res.type == 'success') {
            const {
              userInfo,
              userId,
              token
            } = res.data
            _this.globalData.uuid = userId
            _this.globalData.token = token
            _this.globalData.userInfo = userInfo
          }
          _this.QRCODELOGIN = false
          if (_this.APP_TYPE > 0) {
            getMchInfo({ xuid: _this.globalData.xuid }).then(res => {
              if (res.data != null && res.data.sysMch != null) {
                _this.globalData.mchId = res.data.sysMch.id
                _this.globalData.mchName = res.data.sysMch.name
                _this.globalData.cityName = res.data.cityName
                _this.globalData.location = res.data.locked;
              }
              if (_this.userInfoReadyCallback) {
                _this.globalData.loaded = true;
                _this.userInfoReadyCallback(res)
              }
            }).catch(error => {
              console.log(error)
            })
            console.log('2')
          } else {
            if (_this.userInfoReadyCallback) {
              _this.globalData.location = true;
              _this.globalData.loaded = true;
              _this.userInfoReadyCallback(res)
            }
          }

          getInfo().then(res => {
            if (res.type == 'success') {
              _this.globalData.userVipType = res.data.member.vipType
            }
          }
          ).catch(error => {
            console.log(error)
          })
        }
        )
      },
      fail: err => {
        if (_this.APP_TYPE > 0) {
          if (_this.userInfoReadyCallback) {
            console.log('查看是否有调用3')
            _this.globalData.loaded = true;
            _this.userInfoReadyCallback(err)
          }
          console.log('5')
        } else {
          if (_this.userInfoReadyCallback) {
            _this.globalData.loaded = true;
            _this.globalData.location = true;
            console.log('查看是否有调用4')
            _this.userInfoReadyCallback(err)
          }
          console.log('6')
        }
      }
    })

  },
  onShow(options) {
    // if ((options.scene===1037||options.scene===1038 )&& !util.isNull(options.referrerInfo.extraData)) {
    //   const {extraData} = options.referrerInfo
    //   if (extraData.type == 'backMiniProgram') {
    //     this.event.emit('miniAppData', extraData.data)
    //   }
    // }
  },
  // APP_BASE_API: 'https://g10234.rzico.com:8443',

  APP_BASE_API: 'https://api.rzico.com:8443',

  // APP_BASE_API: 'https://boot.rzico.com:9090',
  // APP_BASE_API: 'https://192.168.2.232:8080',
  // APP_BASE_API: 'https://boot.hulu.fj.cn:8080',
  // APP_BASE_API: 'https://dev.hulu.fj.cn:8443',
  // APP_BASE_API: 'https://app.zenofresh.com:8443',
  // APP_BASE_API: 'https://api.h0594.net:8443',
  // APP_BASE_API:'https://192.168.2.232:7071',
  // APP_BASE_API:'http://175.24.188.209:7071',
  // APP_BASE_API:'http://127.0.0.1:7071',
  // APP_BASE_API: 'http://175.24.188.209:8080',

  // 0 商户版 1.平台版
  APP_TYPE: 0,

  // 旧： 0 会员卡、金币、福利卡 1.钱包、空桶压金、积分 2.钱包、会员卡、积分 3.金币、提货券、积分,4.积分，钱包，库存
  // 新： 0 会员卡、红包、兑换劵 、拼团劵  经典  1 红包、兑换劵 、拼团劵（福利）、空桶压金  2 会员卡、红包、兑换劵 、福利卡  3 （会员卡、福利卡、红包、兑换劵 、拼团劵、空桶压金）全部
  VERSION: 1,

  // 更改行业版本请将 app.json 中 tabBar 的 list 替换为： app.std.json / app.water.json 中的 list
  // 1:标准版,2:水站版,3:餐饮版,4.商超版,5.景区版
  SCOPE_TYPE: 1,

  // 0不开启（礼品卡）
  GIFT_TYPE: 0,

  // 0.不能退货,1.正常退货,2.七天无理由
  ASKFOR_TYPE: 1,

  //0:不开启桌号，1:开启桌号
  DESK_TYPE: 0,

  //0:不开启预约时间，1:开启预约
  DELIVERY_TIME: 0,

  ISV_ID: mchInfoObj.isvId || mchInfoObj.mchId,
  SHARE_TYPE: 0, //0:公共流量   1：私域流量
  FUNS_TYPE: "0",
  QRCODELOGIN: false,
  globalData: {
    mchId: mchInfoObj.mchId, //芸订水  wx98ca23561599628f
    mchName: mchInfoObj.mchName,

    location: false,
    uuid: null,
    token: null,
    userInfo: { id: '' },
    xuid: null,
    shopId: null,
    shopName: null,
    loaded: false,

    offlinePayment: false,
    onlinePayment: true,
    yunchang: false,
    giftCard: false,
    invoiced: false,
    realAuth: false,  //实名认证
    placeOrder: false,  //代客下单
    nameAvatr: false,  //头像昵称必填
    userVipType: 0,  //用户vip类型
  },
  //获取可视窗口宽度
  screenSize: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        if (res.platform == 'ios') {
          that.globalData.platform = 'ios'
        }  //ios
        if (res.platform == 'android') {
          that.globalData.platform = 'android'
        } //安卓
        if (res.platform == 'devtools') {
          that.globalData.platform = 'devtools'
        } //开发者工具

        //可视窗口宽度
        var ww = res.windowWidth;
        //可视窗口高度
        var hh = res.windowHeight;
        that.globalData.ww = ww;
        that.globalData.hh = hh;
        //导航高度
        let titleH;
        if (res['system'].indexOf('Android') > 0) {
          titleH = 48
        } else {
          titleH = 44
        }
        //底部安全距离
        that.globalData.bottomHeight = res.screenHeight - res.safeArea.bottom
        //设备像素比
        that.globalData.pixelRatio = res.pixelRatio
        let height = titleH + res['statusBarHeight'];
        that.globalData.tabbarHeight = -(res.screenHeight - res.windowHeight - height - 48) / 2
      }
    })
  },
  getGeoCode: function (lat, lng) {
    return new Promise(function (resolve, reject) {
      geoCode({
        lat: lat,
        lng: lng,
      }).then(objet => {
        if (objet.type == "success") {
          resolve(objet);
        } else {
          reject(err)
        }
      })
    })
  },
})
